package com.asianrapid.controller.sys;

import com.asianrapid.model.sys.MoneyEntity;
import com.asianrapid.model.sys.PageResult;
import com.asianrapid.service.sys.MoneyService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class MoneyController {

    private Logger log = LoggerFactory.getLogger(MoneyController.class);

    @Resource(name = "moneyServiceImpl")
    private MoneyService moneyService;

    @GetMapping("/money/{name}")
    public MoneyEntity moneysGet(@PathVariable String name) {
        MoneyEntity moneyEntity = moneyService.getMoneyEntityByLoginName(name);
        log.debug("The method is ending");
        return moneyEntity;
    }


    /**
     * 获取user表数据
     *
     * @param name
     * @param pageSize
     * @param page
     * @return
     */
    @GetMapping("/moneys")
    public PageResult usersList(String name, int pageSize, int page) {
        PageResult pageResult = new PageResult();
        pageResult.setData(moneyService.moneysList(name, pageSize, page * pageSize));
        pageResult.setTotalCount(moneyService.moneysSize(name, pageSize, page * pageSize));
        log.debug("The method is ending");
        return pageResult;
    }


    /**
     * 新建用户信息
     *
     * @param moneyEntity
     * @return
     */
    @PostMapping("/moneys/money")
    public MoneyEntity insertUser(@RequestBody MoneyEntity moneyEntity) {
        moneyService.insertMoney(moneyEntity);
        log.debug("The method is ending");
        return moneyEntity;
    }


    /**
     * 更新用户信息
     *
     * @param userEntity
     * @param id
     * @return
     */
    @PutMapping("/moneys/{id}")
    public MoneyEntity updateMoney(@RequestBody MoneyEntity moneyEntity, @PathVariable int id) {
        if (moneyEntity.getId() == id) {
            moneyService.updateMoney(moneyEntity);
        }
        log.debug("The method is ending");
        return moneyEntity;
    }

    /**
     * 删除用户信息
     *
     * @param groupId
     * @return
     */
    @DeleteMapping("/moneys")
    public List<String> deleteMoneys(@RequestBody List<String> groupId) {
        moneyService.deleteMoneys(groupId);
        return groupId;
    }

}
